package com.aimuceliang.aimuceliang.ui;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.aimuceliang.aimuceliang.AppManager;
import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.utils.FastDoubleClickUtils;
import com.aimuceliang.aimuceliang.utils.ScreenUtils;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by ziabo on 2017/7/25.
 * activity的超类
 */

public abstract class BaseActivity extends RxAppCompatActivity implements View.OnClickListener {

    protected String TAG = this.getClass().getSimpleName();

    private CompositeSubscription mCompositeSubscription;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        initStatus();
        setContentView(initContentView());
        ButterKnife.bind(this);
        loadTitle();
        initUIAndListener();
        initData();
    }

    private void loadTitle() {
        View vAdd = findViewById(R.id.v_add);
        ImageView vIvMainLeft = (ImageView) findViewById(R.id.main_iv_left);
        if (vAdd != null) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {//处理沉浸式状态栏
                vAdd.setVisibility(View.GONE);
            } else {
                vAdd.getLayoutParams().height = ScreenUtils.getStatusHeight(this);
            }
        }
        if (vIvMainLeft != null)
            vIvMainLeft.setOnClickListener(this);
    }

    protected abstract int initContentView();

    protected abstract void initUIAndListener();

    protected abstract void initData();

    protected void setTitle(String title) {
        TextView vTvMainTitle = (TextView) findViewById(R.id.main_tv_title);
        if (vTvMainTitle != null) {
            vTvMainTitle.setText(title);
        }
    }


    private void initStatus() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 屏幕竖屏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_iv_left:
                finish();
                break;
        }
    }

    /**
     * 跳转一个界面不传递数据
     *
     * @param clazz 要启动的Activity
     */
    protected void startActivity(Class<? extends BaseActivity> clazz) {
        Intent intent = new Intent();
        intent.setClass(this, clazz);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        if (mCompositeSubscription != null && !mCompositeSubscription.isUnsubscribed()) {
            mCompositeSubscription.unsubscribe();
        }
        AppManager.getAppManager().finishActivity(this);
        super.onDestroy();
    }

    /**
     * 判断触摸时间派发间隔
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (FastDoubleClickUtils.isFastDoubleClick()) {
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    protected void addSubscription(Subscription subscription) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(subscription);
    }
}
